package com.qf.dao.Impl;

import com.qf.dao.OrderDao;
import com.qf.domain.Order;
import com.qf.utils.DataSourceUtil;
import com.qf.utils.TxUtils;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {


    @Override
    public Long findCountByAid(String id) {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = "select count(0) from tb_order where aid=?";
        try {
            Long query = (Long) queryRunner.query(sql, new ScalarHandler(), id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询订单数量失败");
        }
        return null;
    }

    @Override
    public void insert(Order order) {
        Connection conn = TxUtils.getConn();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "INSERT INTO `tb_order` (`id`, `uid`, `money`, `status`, `time`, `aid`) VALUES (?, ?, ?, ?, ?, ?)";
        try {
            queryRunner.update(conn, sql , order.getId(),order.getUid(), order.getMoney(), order.getStatus(), order.getTime(),order.getAid());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加订单失败!!");
        }

    }

    @Override
    public List<Order> findAllorder() {
        QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSource());
        String sql = " SELECT * FROM `tb_order` ";
        try {
            List<Order> query = queryRunner.query(sql, new BeanListHandler<>(Order.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询失败!!");
        }
        return null;
    }
}

